package com.anda.logistics.map.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import com.anda.logistics.model.DriverLocation;

public class GoogleGeocode {
	public static void geocode(double lng, double lat){
		System.out.println("geocode");
		StringBuffer uri = new StringBuffer();
		uri.append("http://maps.google.com/maps/api/geocode/json?language=zh-CN&region=cn&sensor=false")
		   .append("&latlng=").append(lng).append(",").append(lat);
		HttpGet httpGet = new HttpGet(uri.toString());
		HttpClient client=new DefaultHttpClient();
		HttpResponse httpResponse;
		try {
			httpResponse=client.execute(httpGet);
			HttpEntity httpEntity=httpResponse.getEntity();
			String content = HTMLUtil.getContent(httpEntity);
			System.out.println( content );
			//JsonObject jsonObject=new JsonObject();
			JSONObject jsonObject = new JSONObject(content);
			System.out.println(jsonObject.get("status"));
			JSONArray results = jsonObject.getJSONArray("results");
			if (results.length() > 0) {
				JSONArray address_components = results.getJSONObject(0).getJSONArray("address_components");
				for(int i=0; i< address_components.length(); i++){
					JSONObject jo = address_components.getJSONObject(i);
					System.out.println(jo.get("types"));
					System.out.println(jo.get("short_name"));
					System.out.println(jo.get("long_name"));
				}
			}
		} 
		catch (ClientProtocolException e)
		{
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public static DriverLocation getLocationByLatLng(double lat, double lng){
		DriverLocation location = null;
		JSONObject entityObj = new JSONObject();
		try {
			entityObj.put("version", "1.1.0");
			entityObj.put("host", "maps.google.com");
			entityObj.put("request_address", true);
			entityObj.put("address_language", "zh_CN");
			
			JSONObject locationObject = new JSONObject();
			locationObject.put("latitude", lat);
			locationObject.put("longitude", lng);
			
			entityObj.put("location", locationObject);
			System.out.println("---------entity:------"+entityObj);
			
			DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost("http://www.google.com/loc/json");
			httpPost.setEntity(new StringEntity(entityObj.toString()));
			HttpResponse response = defaultHttpClient.execute(httpPost);
			//Header header = response.getEntity().getContentType();
			//System.out.println(header);
			//System.out.println(HTMLUtil.getContent(response.getEntity()));
			String s = HTMLUtil.getContent(response.getEntity());
//			BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
//			StringBuffer sb = new StringBuffer();				
//			String s ;
//			while ( (s=br.readLine()) != null){		
//				sb.append(s);
//			}				
//			br.close();
			System.out.println("--------json result:-------"+s.toString());
			JSONObject resultObj = new JSONObject(s.toString());				
			JSONObject locationObj = resultObj.getJSONObject("location");
			JSONObject addressObj = locationObj.getJSONObject("address");
			
			location = new DriverLocation();
			
			location.setLat(locationObj.getDouble("latitude"));
			location.setLng(locationObj.getDouble("longitude"));				
			location.setCity(addressObj.getString("city"));
			location.setCountry(addressObj.getString("country"));
			location.setCountryCode(addressObj.getString("country_code"));
			location.setRegion(addressObj.getString("region"));
			if (resultObj.has("access_token")){
				location.setAccessToken(resultObj.getString("access_token"));
			}
			if (locationObj.has("accuracy")){
				location.setAccuracy((float) locationObj.getDouble("accuracy"));
			}
			if ( addressObj.has("street") ){
				location.setStreet(addressObj.getString("street"));
			}
			if ( addressObj.has("street_number") ){
				location.setStreetNumber(addressObj.getString("street_number"));
			}
		} catch (Exception e){
			e.printStackTrace();
			location = null;
		}

		return location;
	}
	
	public static void main(String [] args){
		System.out.println(getLocationByLatLng(25.85D, 98.85D));
	}
}
